﻿using System;
using System.Html;
using enyo;
using onyx;

namespace JenkinsView
{
    public class ServerEditDialog : onyx.Popup
    {
        [EvalAtCompile, NoCompile]
        private const string LBL_ERR = "lblErr";
        [EvalAtCompile, NoCompile]
        private const string TXT_SVR = "txtSvr";
        [EvalAtCompile, NoCompile]
        private const string TXT_URL = "txtUrl";
        [EvalAtCompile, NoCompile]
        private const string TXT_USR = "txtUsr";
        [EvalAtCompile, NoCompile]
        private const string TXT_PWD = "txtPwd";
        
        private new Component[] Components = new Component[]
        {
            new Control(){ Content = "Input Server Information:" },
            new Control(){ Name = LBL_ERR, Classes = CssConst.VAL_ERR, AllowHtml = true, Content = "&nbsp;" },
            new Control(){ Tag = "br" },
            new onyx.InputDecorator(){ Kind = onyx.ComponentKind.InputDecorator,
                Components = new Component[]
                {
                    new onyx.Input() { Kind = onyx.ComponentKind.Input, Name = TXT_SVR, Placeholder = "Unique name" }
                }
            },
            new Control(){Tag = "br"},
            new onyx.InputDecorator(){ Kind = onyx.ComponentKind.InputDecorator,
                Components = new Component[]
                {
                    new onyx.Input() { Kind = onyx.ComponentKind.Input, Name = TXT_URL, Placeholder = "Url" }
                }
            },
            new Control(){Tag = "br"},
            new onyx.InputDecorator(){ Kind = onyx.ComponentKind.InputDecorator,
                Components = new Component[]
                {
                    new onyx.Input() { Kind = onyx.ComponentKind.Input, Name = TXT_USR, Placeholder = "Username" }
                }
            },
            new Control(){Tag = "br"},
            new onyx.InputDecorator(){ Kind = onyx.ComponentKind.InputDecorator,
                Components = new Component[]
                {
                    new onyx.Input() { Kind = onyx.ComponentKind.Input, Name = TXT_PWD, Placeholder = "Password" }
                }
            },
            new Control(){ Tag = "br"},
            new Control(){ AllowHtml = true, Content = "Leave Username/Password<br>blank to access as anonymous.", Classes = CssConst.SVR_HINT },
            new Control(){ Tag = "br"},
            new onyx.Button() { Kind = onyx.ComponentKind.Button, Content = "OK", Classes = CssConst.ONYX_DARK, ontap = "saveEdit" },
            new onyx.Button() { Kind = onyx.ComponentKind.Button, Content = "Cancel", Classes = CssConst.ONYX_DARK, ontap = "abortEdit" }
        };

        private C FindControl<C>(string name) where C : class
        {
            return this._[name] as C;
        }

        private void SaveEdit()
        {
            var svr = new JenkinsServer();
            svr.SvrName = this.FindControl<onyx.Input>(TXT_SVR).Value;
            svr.Url = this.FindControl<onyx.Input>(TXT_URL).Value;
            svr.User = this.FindControl<onyx.Input>(TXT_USR).Value;
            svr.Password = this.FindControl<onyx.Input>(TXT_PWD).Value;

            var lblErr = this.FindControl<Control>(LBL_ERR);
            string result = DomainModel.Model.ServerManager.AppendServer(svr);
            if (result == null)
            {
                lblErr.Content = "&nbsp;";
                this.Hide();
            }
            else
            {
                lblErr.Content = result;
            }
        }

        private void AbortEdit()
        {
            this.Hide();
        }

        public void Clear()
        {
            var txtSvr = this.FindControl<onyx.Input>(TXT_SVR);
            txtSvr.Value = null;
            var txtUrl = this.FindControl<onyx.Input>(TXT_URL);
            txtUrl.Value = null;
            var txtUsr = this.FindControl<onyx.Input>(TXT_USR);
            txtUsr.Value = null;
            var txtPwd = this.FindControl<onyx.Input>(TXT_PWD);
            txtPwd.Value = null;
        }
    }
}
